package com.javafactroy.kunlong.domain.generalquery.agg;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.javafactroy.kunlong.data.dto.QueryConditionApiDto;
import com.javafactroy.kunlong.data.entity.QueryMasterApiEntity;
import com.javafactroy.kunlong.domain.generalquery.valueobj.OrderBysValueObj;
import com.javafactroy.kunlong.domain.generalquery.valueobj.QueryConditionParam;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Data
@Slf4j
@Accessors(chain = true)
public class QueryMasterAgg {
    public Long masterId;

    Integer pageSize = 1000;
    Integer pageNum = 1;
    OrderBysValueObj orderBys;

    List<QueryConditionParam> queryConditionParams;

    QueryMasterApiEntity queryMasterApiEntity;
    List<QueryConditionApiDto> queryConditionApiDtos;
    Map<String, Object> queryParams;

    public void check() {

    }

    public QueryMasterAgg parseParams() {
        queryConditionParams = new ArrayList<>();
        queryConditionApiDtos.forEach(i -> {
            Optional<QueryConditionParam> optional = i.parseParam();
            if (optional.isPresent()) {
                queryConditionParams.add(optional.get());
            }

        });
        log.info("param={}", queryConditionParams);

        return this;
    }

    public QueryMasterAgg putParam(String paramKey, Object paramValue) {
        if (ObjectUtil.isEmpty(queryParams)) {
            queryParams = new LinkedHashMap<>();

        }
        queryParams.put(paramKey, paramValue);
        return this;
    }

    public String buildSelectSql() {
        StringBuilder sb = new StringBuilder(122);

        sb.append(StrUtil.format("select {} from ", "*"));
        sb.append(queryMasterApiEntity.getTableName());
        sb.append(" where 1=1 ");
        if (ObjectUtil.isNotEmpty(queryConditionApiDtos)) {
            queryConditionApiDtos.forEach(
                    i -> sb.append(" and ").append(i.toWhere())
            );
        }
        sb.append(" limit ").append(pageSize);
        sb.append(" offset ").append((pageNum - 1) * pageNum);
        String sql = sb.toString();
        for (String key : queryParams.keySet()) {
            sql = StrUtil.replace(sql, key, queryParams.get(key).toString());
        }
        return sql;
    }
    public String buildSelectSqlAlias(String alias) {
        StringBuilder sb = new StringBuilder(122);

        sb.append(StrUtil.format("select {} from ", alias+".*"));
        sb.append(queryMasterApiEntity.getTableName());
        sb.append(" where 1=1 ");
        if (ObjectUtil.isNotEmpty(queryConditionApiDtos)) {
            queryConditionApiDtos.forEach(
                    i -> sb.append(" and ").append(i.toWhere())
            );
        }
        sb.append(" limit ").append(pageSize);
        sb.append(" offset ").append((pageNum - 1) * pageNum);
        String sql = sb.toString();
        for (String key : queryParams.keySet()) {
            sql = StrUtil.replace(sql, key, queryParams.get(key).toString());
        }
        return sql;
    }

}
